<template>
    <el-dialog :title="form.cardId ? '编辑' : '新增'" v-model="visible"
      :close-on-click-modal="false" draggable>
      <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
       <el-row :gutter="24">
    <el-col :span="12" class="mb20">
      <el-form-item label="买家id" prop="buyerId">
        <el-input v-model="form.buyerId" placeholder="请输入买家id"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="支付方式id" prop="methodId">
        <el-input v-model="form.methodId" placeholder="请输入支付方式id"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="支付方式编码" prop="methodCd">
        <el-input v-model="form.methodCd" placeholder="请输入支付方式编码"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="通道ID" prop="wayId">
        <el-input v-model="form.wayId" placeholder="请输入通道ID"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="支付通道编码" prop="wayCd">
        <el-input v-model="form.wayCd" placeholder="请输入支付通道编码"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="通道名称" prop="wayName">
        <el-input v-model="form.wayName" placeholder="请输入通道名称"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="银行卡号" prop="cardNumber">
        <el-input v-model="form.cardNumber" placeholder="请输入银行卡号"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="持卡人姓名" prop="cardholderName">
        <el-input v-model="form.cardholderName" placeholder="请输入持卡人姓名"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="银行预留手机号" prop="phoneNumber">
        <el-input v-model="form.phoneNumber" placeholder="请输入银行预留手机号"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="银行编码" prop="bankNo">
        <el-input v-model="form.bankNo" placeholder="请输入银行编码"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="开户行" prop="bankName">
        <el-input v-model="form.bankName" placeholder="请输入开户行"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="状态" prop="status">
        <el-input v-model="form.status" placeholder="请输入状态"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="创建时间" prop="createAt">
        <el-input v-model="form.createAt" placeholder="请输入创建时间"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="更新时间" prop="updateAt">
        <el-input v-model="form.updateAt" placeholder="请输入更新时间"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="租户" prop="tanentId">
        <el-input v-model="form.tanentId" placeholder="请输入租户"/>
      </el-form-item>
      </el-col>

			</el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script setup lang="ts" name="BankCardDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj, validateExist } from '/@/api/payment/BankCard'
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典

// 提交表单数据
const form = reactive({
		cardId:'',
	  buyerId: '',
	  methodId: '',
	  methodCd: '',
	  wayId: '',
	  wayCd: '',
	  wayName: '',
	  cardNumber: '',
	  cardholderName: '',
	  phoneNumber: '',
	  bankNo: '',
	  bankName: '',
	  status: '',
	  createAt: '',
	  updateAt: '',
	  tanentId: '',
});

// 定义校验规则
const dataRules = ref({
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.cardId = ''

  // 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

  // 获取bankCard信息
  if (id) {
    form.cardId = id
    getBankCardData(id)
  }
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
    loading.value = true;
		form.cardId ? await putObj(form) : await addObj(form);
		useMessage().success(form.cardId ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getBankCardData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj({cardId: id}).then((res: any) => {
    Object.assign(form, res.data[0])
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>
